<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>解决新浪/微博图床图片403错误防盗链问题 | 山岚 - 90码农历险记</title>
  <meta name="description" content="缘由微博图床是网友利用新浪微博上传图片的功能，然后网友将图片的链接进行分享。微博图床的稳定性不用我多介绍，但是由于愈来愈多的网友使用微博做图床，微博官方可能觉得影响服务器或者费用的支出等原因，开始对微博图片添加了防盗链的功能。 新浪微博的图片防盗链：它的防盗链原理较为简单，只要不是新浪微博的白名单域名请求该图片，服务器会返回一个403的错误码。背后的原理是利用了浏览器的HTTP请求头默认会携带：R">
<meta name="keywords" content="Hexo">
<meta property="og:type" content="article">
<meta property="og:title" content="解决新浪&#x2F;微博图床图片403错误防盗链问题">
<meta property="og:url" content="https://blog.gobyte.cn/post/cfce32d8.html">
<meta property="og:site_name" content="山岚博客">
<meta property="og:description" content="缘由微博图床是网友利用新浪微博上传图片的功能，然后网友将图片的链接进行分享。微博图床的稳定性不用我多介绍，但是由于愈来愈多的网友使用微博做图床，微博官方可能觉得影响服务器或者费用的支出等原因，开始对微博图片添加了防盗链的功能。 新浪微博的图片防盗链：它的防盗链原理较为简单，只要不是新浪微博的白名单域名请求该图片，服务器会返回一个403的错误码。背后的原理是利用了浏览器的HTTP请求头默认会携带：R">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://wx3.sinaimg.cn/large/96e311f0gy1g4mvshden7j20tc0c9dgv.jpg">
<meta property="og:image" content="https://wx1.sinaimg.cn/large/96e311f0gy1g4mwv5dsv0j20qg091q3f.jpg">
<meta property="og:image" content="https://wx1.sinaimg.cn/large/96e311f0gy1g4mwwjh95fj20ow08nwf0.jpg">
<meta property="og:image" content="https://wx2.sinaimg.cn/large/96e311f0gy1g4mwxb4ac4j20ny07xmxo.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/large/96e311f0gy1g4mxliqygdj20pr0jmmyy.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/96e311f0gy1g4mxkwc96rj212l0nsn2l.jpg">
<meta property="og:image" content="https://wx2.sinaimg.cn/large/96e311f0gy1g4my116m6wj20xg0ovgpq.jpg">
<meta property="og:updated_time" content="2019-12-10T08:14:59.826Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="解决新浪&#x2F;微博图床图片403错误防盗链问题">
<meta name="twitter:description" content="缘由微博图床是网友利用新浪微博上传图片的功能，然后网友将图片的链接进行分享。微博图床的稳定性不用我多介绍，但是由于愈来愈多的网友使用微博做图床，微博官方可能觉得影响服务器或者费用的支出等原因，开始对微博图片添加了防盗链的功能。 新浪微博的图片防盗链：它的防盗链原理较为简单，只要不是新浪微博的白名单域名请求该图片，服务器会返回一个403的错误码。背后的原理是利用了浏览器的HTTP请求头默认会携带：R">
<meta name="twitter:image" content="https://wx3.sinaimg.cn/large/96e311f0gy1g4mvshden7j20tc0c9dgv.jpg">
  <!-- Canonical links -->
  <link rel="canonical" href="https://blog.gobyte.cn/post/cfce32d8.html">
  
    <link rel="alternate" href="/atom.xml" title="山岚博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/shan-2.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/css/style.css">
  
  
  
  
</head>


<body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/shanlanCoding" target="_blank">
          <img class="img-circle img-rotate" src="/images/ha-002-small.png" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">山岚</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">半吊子全栈开发者</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Work in 0755; Home is in 0713</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/links/">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">友链</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/shanlanCoding" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=CWRgen1se2phZnxJeHgnamZk" target="_blank" title="Email" data-toggle=tooltip data-placement=top><i class="icon icon-email"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验，知无不言言无不尽!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/JavaScript/">JavaScript</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/windows/">windows</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/前端/">前端</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/博客优化/">博客优化</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/变得更好/">变得更好</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/心情/">心情</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/手机/">手机</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/服务器/">服务器</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/求职/">求职</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/骚操作/">骚操作</a><span class="category-list-count">3</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/AutoJs/">AutoJs</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Git/">Git</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hexo/">Hexo</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java/">Java</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Java面试题/">Java面试题</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/">React</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React教程/">React教程</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/centOS/">centOS</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java基础/">java基础</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java集合框架/">java集合框架</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vue/">vue</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/windows-10/">windows 10</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/人脸识别/">人脸识别</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/博客优化/">博客优化</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/吐槽/">吐槽</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/小米6/">小米6</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/思维改变/">思维改变</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/自定义手机号/">自定义手机号</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/tags/AutoJs/" style="font-size: 13.2px;">AutoJs</a> <a href="/tags/Git/" style="font-size: 13.4px;">Git</a> <a href="/tags/Hexo/" style="font-size: 13.8px;">Hexo</a> <a href="/tags/Java/" style="font-size: 13px;">Java</a> <a href="/tags/JavaScript/" style="font-size: 13.2px;">JavaScript</a> <a href="/tags/Java面试题/" style="font-size: 13.6px;">Java面试题</a> <a href="/tags/React/" style="font-size: 14px;">React</a> <a href="/tags/React教程/" style="font-size: 13px;">React教程</a> <a href="/tags/centOS/" style="font-size: 13px;">centOS</a> <a href="/tags/java基础/" style="font-size: 13px;">java基础</a> <a href="/tags/java集合框架/" style="font-size: 13px;">java集合框架</a> <a href="/tags/linux/" style="font-size: 13px;">linux</a> <a href="/tags/nginx/" style="font-size: 13.2px;">nginx</a> <a href="/tags/vue/" style="font-size: 13px;">vue</a> <a href="/tags/windows-10/" style="font-size: 13px;">windows 10</a> <a href="/tags/人脸识别/" style="font-size: 13px;">人脸识别</a> <a href="/tags/博客优化/" style="font-size: 13px;">博客优化</a> <a href="/tags/吐槽/" style="font-size: 13px;">吐槽</a> <a href="/tags/小米6/" style="font-size: 13px;">小米6</a> <a href="/tags/思维改变/" style="font-size: 13px;">思维改变</a> <a href="/tags/自定义手机号/" style="font-size: 13px;">自定义手机号</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/12/">十二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">十一月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">十月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">九月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">八月 2019</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">9</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/前端/">前端</a>
              </p>
              <p class="item-title">
                <a href="/post/7080982b.html" class="title">AntD的一个警告解决方法：defaultvalue is invalid for getfielddecorator will set value please use option.initialvalue instead</a>
              </p>
              <p class="item-date">
                <time datetime="2019-12-07T08:58:03.000Z" itemprop="datePublished">2019-12-07</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/JavaScript/">JavaScript</a>
              </p>
              <p class="item-title">
                <a href="/post/cbc43a97.html" class="title">海信A5自动阅读打卡签到工具软件脚本</a>
              </p>
              <p class="item-date">
                <time datetime="2019-12-06T09:03:39.000Z" itemprop="datePublished">2019-12-06</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/JavaScript/">JavaScript</a>
              </p>
              <p class="item-title">
                <a href="/post/ae3d322e.html" class="title"> AutoJs获取的Text文本是乱码|Android爬虫反字体屏蔽乱码解决方案 </a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-23T03:50:19.000Z" itemprop="datePublished">2019-11-23</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/服务器/">服务器</a>
              </p>
              <p class="item-title">
                <a href="/post/1a22163b.html" class="title">GitHub克隆clone太慢添加代理加速访问</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-13T03:51:13.000Z" itemprop="datePublished">2019-11-13</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/心情/">心情</a>
              </p>
              <p class="item-title">
                <a href="/post/d90e4430.html" class="title">奇葩公司每周晚上占用下班时间开会、团建（跑步）</a>
              </p>
              <p class="item-date">
                <time datetime="2019-11-11T06:36:16.000Z" itemprop="datePublished">2019-11-11</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#缘由"><span class="toc-number">1.</span> <span class="toc-text">缘由</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#新浪微博的图片防盗链："><span class="toc-number">1.0.1.</span> <span class="toc-text">新浪微博的图片防盗链：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解决方法1（不推荐）"><span class="toc-number">1.0.2.</span> <span class="toc-text">解决方法1（不推荐）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解决方法2-给图片添加referrerpolicy-“no-referrer”（推荐）"><span class="toc-number">1.0.3.</span> <span class="toc-text">解决方法2-给图片添加referrerpolicy=“no-referrer”（推荐）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#referrerPolicy可选值："><span class="toc-number">1.0.3.1.</span> <span class="toc-text">referrerPolicy可选值：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#操作方法："><span class="toc-number">1.0.3.2.</span> <span class="toc-text">操作方法：</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#解决方法3-在生成HTML的时候加上referrerpolicy-”referrerpolicy”"><span class="toc-number">1.0.4.</span> <span class="toc-text">解决方法3-在生成HTML的时候加上referrerpolicy=”referrerpolicy”</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#页面地址为：点我打开效果页面，效果图如下："><span class="toc-number">1.1.</span> <span class="toc-text">页面地址为：点我打开效果页面，效果图如下：</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#总结："><span class="toc-number">2.</span> <span class="toc-text">总结：</span></a></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-解决新浪-微博图床图片403错误防盗链问题" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header" name="标题头部>
      
        
  
    <h1 class="article-title" itemprop="name">
      解决新浪/微博图床图片403错误防盗链问题
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/post/cfce32d8.html" class="article-date">
	  <time datetime="2019-07-03T11:29:13.000Z" itemprop="datePublished">2019-07-03</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/categories/博客优化/">博客优化</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/tags/Hexo/">Hexo</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


	<span class="article-read hidden-xs">
    	<i class="icon icon-eye-fill" aria-hidden="true"></i>
    	<span id="/post/cfce32d8.html" class="leancloud_visitors"  data-flag-title="解决新浪/微博图床图片403错误防盗链问题">
			<span class="leancloud-visitors-count">0</span>
		</span>
    </span>

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/post/cfce32d8.html#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody" name="正文">
      
        <h1 id="缘由"><a href="#缘由" class="headerlink" title="缘由"></a>缘由</h1><p>微博图床是网友利用新浪微博上传图片的功能，然后网友将图片的链接进行分享。微博图床的稳定性不用我多介绍，但是由于愈来愈多的网友使用微博做图床，微博官方可能觉得影响服务器或者费用的支出等原因，开始对微博图片添加了防盗链的功能。</p>
<h3 id="新浪微博的图片防盗链："><a href="#新浪微博的图片防盗链：" class="headerlink" title="新浪微博的图片防盗链："></a>新浪微博的图片防盗链：</h3><p>它的防盗链原理较为简单，只要不是新浪微博的白名单域名请求该图片，服务器会返回一个403的错误码。背后的原理是利用了浏览器的HTTP请求头默认会携带：<code>Referer</code>属性。而这个属性记录了该请求由哪个域名发送的。所以微博图床直接校验该属性的值，只要不是微博允许的域名访问图片链接，将会不予返回图片。下图是我博客请求一张图片的请求头：</p>
<p><img src="//wx3.sinaimg.cn/large/96e311f0gy1g4mvshden7j20tc0c9dgv.jpg" alt="Referer请求头"></p>
<p>可以看到该请求是由：<a href="https://blog.gobyte.cn/post/f9458eff.html">https://blog.gobyte.cn/post/f9458eff.html</a>发起的，而且这个<code>Referer</code>属性是浏览器默认携带，既然是默认携带，那肯定有关闭的方法。</p>
<h3 id="解决方法1（不推荐）"><a href="#解决方法1（不推荐）" class="headerlink" title="解决方法1（不推荐）"></a>解决方法1（不推荐）</h3><p>在HTML的<code>&lt;head&gt;</code>标签里添加mate标签，如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"referrer"</span> <span class="attr">content</span>=<span class="string">"no-referrer"</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>不推荐该方法的原因是会导致该页面的所有的HTTP请求都不会携带<code>Referer</code>属性。这会导致部分功能失效，如某些统计信息失效，比如我使用了“不蒜子”的统计功能，会导致失效，所以我推荐方法2.</p>
<h3 id="解决方法2-给图片添加referrerpolicy-“no-referrer”（推荐）"><a href="#解决方法2-给图片添加referrerpolicy-“no-referrer”（推荐）" class="headerlink" title="解决方法2-给图片添加referrerpolicy=“no-referrer”（推荐）"></a>解决方法2-给图片添加referrerpolicy=“no-referrer”（推荐）</h3><p>既然不能给所有的HTTP请求添加<code>content=&quot;no-referrer&quot;</code>，那么我们就仅给图片添加。</p>
<p>在img标签有个属性名叫<code>referrerpolicy</code>，给部分的HTTP请求添加<code>Referer</code>属性。</p>
<p>原生JavaScript语法：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">refStr = imgElt.referrerPolicy;</span><br><span class="line">imgElt.referrerPolicy = refStr;</span><br></pre></td></tr></table></figure>

<h4 id="referrerPolicy可选值："><a href="#referrerPolicy可选值：" class="headerlink" title="referrerPolicy可选值："></a>referrerPolicy可选值：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"no-referrer"</span>：表示HTTP头部信息将不会发送 referrer 。</span><br><span class="line"><span class="string">"origin"</span>：表示 referrer 只包含策略、主机名、端口等页面源的信息。</span><br><span class="line"><span class="string">"unsafe-url"</span>： 这意味着引用者将包括源站和路径（但不包括片段、密码或用户名）。这种情况是不安全的，因为它可能会泄漏路径信息，这些信息已被使用TLS隐藏到第三方。</span><br></pre></td></tr></table></figure>

<p>我对这三个值做了3次测试</p>
<p>​    <code>no-referrer</code>-测试：</p>
<p><img src="//wx1.sinaimg.cn/large/96e311f0gy1g4mwv5dsv0j20qg091q3f.jpg" alt="no-referrer"></p>
<hr>
<p><code>origin</code>-测试：</p>
<p><img src="//wx1.sinaimg.cn/large/96e311f0gy1g4mwwjh95fj20ow08nwf0.jpg" alt="origin"></p>
<hr>
<p><code>unsafe-url</code>-测试：</p>
<p><img src="//wx2.sinaimg.cn/large/96e311f0gy1g4mwxb4ac4j20ny07xmxo.jpg" alt="unsafe-url"></p>
<hr>
<p>三者区别如上面三图所示，符合上面的“referrerPolicy可选值”描述。</p>
<p>所以我们只需要给img标签添加：<code>referrerpolicy=&quot;no-referrer&quot;</code>即可。</p>
<p>但是仅仅不可能每次都去手动添加，所以要么直接修改模板文件，要么写一段JavaScript，让页面加载HTML完成后，再对img标签进行遍历修改，话不多数，以下是我使用jquery完成的代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> link = <span class="string">""</span> ;</span><br><span class="line"><span class="comment">// 遍历所有的img标签</span></span><br><span class="line">$(<span class="string">"img"</span>).each( <span class="function">(<span class="params">i,o</span>) =&gt;</span> &#123;</span><br><span class="line">	<span class="keyword">var</span> o = $(o);</span><br><span class="line">    <span class="comment">// 判断图片的链接是否包含sinaimg关键字</span></span><br><span class="line">	<span class="keyword">if</span>( o.attr(<span class="string">"src"</span>).indexOf(<span class="string">"sinaimg"</span>) &gt; <span class="number">0</span> )&#123;</span><br><span class="line">        <span class="comment">// 给这个标签加上referrerPlicy属性</span></span><br><span class="line">		o.attr(<span class="string">"referrerpolicy"</span>,<span class="string">"no-referrer"</span>);</span><br><span class="line">        <span class="comment">// 备份图片的src</span></span><br><span class="line">		link = o.attr(<span class="string">"src"</span>);</span><br><span class="line">        <span class="comment">// 重新设置src，让页面重新加载一次图片</span></span><br><span class="line">		o.attr(<span class="string">"src"</span>,link);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong>这段JavaScript代码需要放在HTML的底部，所以当HTML第一次加载完图片的时候，不出意外的话console控制台依然会报403错误，因为首次加载图片的时候JavaScript脚本并没有给img添加该属性，所以理所当然的是403。但是如果把JavaScript代码放在页面的顶部，那么会因为图片的img标签晚于JavaScript的执行，导致JavaScript代码找不到img标签会让功能不生效。</p>
<h4 id="操作方法："><a href="#操作方法：" class="headerlink" title="操作方法："></a>操作方法：</h4><ol>
<li><p>找到你的主题的JavaScript代码，我是使用pure主题</p>
</li>
<li><p>我这主题的修改目录：<code>hexo\pure\layout\_common\script.ejs</code>里添加上述代码</p>
</li>
<li><p>最终修改结果如下：<img src="//ws1.sinaimg.cn/large/96e311f0gy1g4mxliqygdj20pr0jmmyy.jpg" alt="源代码效果"><br><img src="//ws4.sinaimg.cn/large/96e311f0gy1g4mxkwc96rj212l0nsn2l.jpg" alt="页面显示效果"></p>
</li>
<li><p>使用其他主题的同学请自行查找对应的文件修改</p>
</li>
</ol>
<p>当然，如果你知道如何修改Hexo的页面模板，直接在模板里给图片标签加上<code>referrerpolicy=&quot;referrerpolicy&quot;</code>也可以，但是我不知道如何修改。所以我还提供了方法3。</p>
<h3 id="解决方法3-在生成HTML的时候加上referrerpolicy-”referrerpolicy”"><a href="#解决方法3-在生成HTML的时候加上referrerpolicy-”referrerpolicy”" class="headerlink" title="解决方法3-在生成HTML的时候加上referrerpolicy=”referrerpolicy”"></a>解决方法3-在生成HTML的时候加上referrerpolicy=”referrerpolicy”</h3><p>注意，因为我是使用Hexo博客程序，该博客是使用markdown编写文章，最后由HEXO程序进行渲染出HTML文件。所以我直接在markdown里标注要需要添加：<code>referrerpolicy=&quot;referrerpolicy&quot;</code>的图片，渲染的时候直接添加该属性。</p>
<p>而Hexo提供了一个自定义函数的功能，操作方法：</p>
<ol>
<li><p>在你的Hexo程序目录的“script”文件夹里（如没有自己新建），创建一个文件“myFunction.js”</p>
</li>
<li><p>复制以下JavaScript代码到该文件内</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 给img添加referer policy 标签，解决referer图片防盗链</span></span><br><span class="line">hexo.extend.tag.register(<span class="string">'s'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">args</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">const</span> image_url = args[<span class="number">0</span>], alt = args[<span class="number">1</span>];</span><br><span class="line">  <span class="keyword">return</span> <span class="string">`&lt;img src="<span class="subst">$&#123;image_url&#125;</span>" alt="<span class="subst">$&#123;alt&#125;</span>" referrerpolicy="no-referrer"&gt;&lt;/img&gt;`</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在markdown里使用 <code>&lt;% s 图片url alt说明 %&gt;</code>调用。调用代码示例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;% s https://ws4.sinaimg.cn/large/96e311f0gy1g3uyql0zj0g20s60lmn71.gif 删除效果 %&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><h2 id="页面地址为：点我打开效果页面，效果图如下："><a href="#页面地址为：点我打开效果页面，效果图如下：" class="headerlink" title="页面地址为：点我打开效果页面，效果图如下："></a>页面地址为：<a href="https://blog.gobyte.cn/post/f9458eff.html">点我打开效果页面</a>，效果图如下：<img src="//wx2.sinaimg.cn/large/96e311f0gy1g4my116m6wj20xg0ovgpq.jpg" alt="效果图"></h2><p><strong>该方法适用于偶尔使用微博图床；</strong>因为使用了自定义语法的功能，导致markdown编写的时候不能预览图片。而且每次书写自定义代码尤为的不方便，我个人不太建议使用。</p>
</li>
</ol>
<h1 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h1><p>微博这次加了图片防盗链，虽然是很简单的防盗措施，但是也说明了微博对于图片外链的态度，不喜欢网友使用他们的服务器架设图床，所以以后也可能会有更加厉害的防盗链方法也是可能的。</p>
<p>我个人建议各位同学还是架设一个自己的文件存储系统，至少图片不会有丢失、无法访问的风险，目前各大云服务提供商都有免费的套餐，大家可以了解一下。</p>
<p>本篇文章也是个人思考的总结，最后感谢你的耐心阅读。</p>

      
    </div>

    <div class="article-footer" name="文章脚部">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://blog.gobyte.cn/post/cfce32d8.html" title="解决新浪/微博图床图片403错误防盗链问题" target="_blank" rel="external">https://blog.gobyte.cn/post/cfce32d8.html</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/shanlanCoding" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/images/ha-002-small.png" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/shanlanCoding" target="_blank"><span class="text-dark">山岚</span><small class="ml-1x">半吊子全栈开发者</small></a></h3>
        <div>大龄萌新；新时代福娃；喜欢东搞西搞；对待问题有独到的见解；</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/post/47dfb631.html" title="Java面试复习巩固"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/post/c621cb41.html" title="spring-boot部署到CentOS服务器、安装Redis、配置Nginx、MySQL"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  
  <div class="bar-right">
    
  </div>
  </div>
</nav>
  



  	<script>
	  <!-- 百度站长自动推送链接 -->
		(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
		})();
	</script>
</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/shanlanCoding" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=CWRgen1se2phZnxJeHgnamZk" target="_blank" title="Email" data-toggle=tooltip data-placement=top><i class="icon icon-email"></i></a></li>
        
    </ul>



	<div class="powered-by" name="不蒜子" sytle="font-size:12px">
		<i class="icon icon-users"></i>
		<span id="busuanzi_container_site_uv">
			总访客 : <span id="busuanzi_value_site_uv"></span>
		</span><br/>
		<i class="icon icon-eye-fill"></i>
		<span id="busuanzi_container_site_pv">
		    总阅读 : <span id="busuanzi_value_site_pv"></span>
		</span>
	</div>
    
	<div class="copyright" name="作者">
		
		<div class="publishby">
			Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
		</div>
	</div>

</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>

<script>
	window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')

		<!-- 百度统计 -script.ejs --> 
		var _hmt = _hmt || [];
		(function() {
		  var hm = document.createElement("script");
		  hm.src = "https://hm.baidu.com/hm.js?e003775b21b75a71e1396c1fc395cce7";
		  var s = document.getElementsByTagName("script")[0]; 
		  s.parentNode.insertBefore(hm, s);
		})();

		<!-- 新浪云防盗链 -->
		var link = "" ;
		setTimeout( function(){
			$("img").each( (i,o) => {
				var o = $(o);
				if(o.attr("src").indexOf("sinaimg") > 0 ){
					o.attr("referrerpolicy","no-referrer");
					link = o.attr("src");
					o.attr("src",link);
				}
			});
		},1000);

</script>

<script src="/js/plugin.min.js"></script>
<script src="/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>






	
   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: 'ksrz2lYRLkGCOqMraVC2gBB3-gzGzoHsz',
    appKey: 'ehlbHl0wgN6gbvN4NQexYxnl',
    placeholder: '说点什么吧？',
    avatar: 'robohash',
    meta: meta,
    pageSize: '10' || 10,
    visitor: true
  });
  </script>

     







</body>
</html>